package com.itlong.cloud.controller.user;

import com.itlong.cloud.POJO.DTO.common.PadFaceSyncDataDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateUserManagePageDTO;
import com.itlong.cloud.POJO.PO.ProjectPO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.app.AppUserOwnProjectVO;
import com.itlong.cloud.POJO.VO.operate.OperateUserManageVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.equip.property.IPadFaceSyncDataServiceClient;
import com.itlong.cloud.project.operate.IOperateProjectServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.app.IAppRegisterServiceClient;
import com.itlong.cloud.user.operate.IOperateUserManageServiceClient;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <desc>
 *      运营—APP用户模块—用户管理。
 * <desc/>
 *
 * @createDate 2017/10/19.
 */
@RestController
@RequestMapping("/appUser/userManage")
public class OperateUserManageController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(OperateUserManageController.class);

    //用户管理
    @Autowired
    IOperateUserManageServiceClient iOperateUserManageServiceClient;

    @Autowired
    IAppRegisterServiceClient iAppRegisterServiceClient;

    @Autowired
    IOperateProjectServiceClient iOperateProjectServiceClient;

    @Autowired
    IPadFaceSyncDataServiceClient iPadFaceSyncDataServiceClient;


    /**
     * <desc>
     *      获取用户详细信息。
     * <desc/>
     *
     * @param  userId  用户ID
     * @return 返回用户详细信息
     * @author shengen.T
     * @createDate 2017/9/22
     */
    @AuthAccessRequired
    @RequestMapping(path="/getUserInfo",method = RequestMethod.POST)
    @ResponseBody
    public Object getUserInfo(String userId){
        OperateUserManageVO userInfo=null;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            userId=request.getAttribute("userId")==null?null:request.getAttribute("userId").toString();

            //判断用户ID
            if(StringUtils.isBlank(userId)) {
                return new MessageVO(OperateErrorCodeEnum.USER_ID_EMPTY_ERR.getErrorCode());
            }
            userInfo= iOperateUserManageServiceClient.getUserInfo(userId);
            return  new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),userInfo);
        } catch (Exception e) {
            throw new DataAccessException("【运营_用户_用户管理】获取用户详细信息失败:",e);
        }
    }

    /**
     * <desc>
     *      获取用户分页数据。
     * <desc/>
     *
     * @param  pageDTO  请求参数对象
     * @return   返回用户信息列表
     * @author shengen.T
     * @createDate 2017/9/22
     */
    @AuthAccessRequired
    @RequestMapping(path="/getUserInfoPage",method = RequestMethod.POST)
    @ResponseBody
    public Object getUserInfopage(OperateUserManagePageDTO pageDTO){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateUserManagePageDTO.class,request);
            if(null==pageDTO) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<OperateUserManageVO> page= iOperateUserManageServiceClient.getUserInfoPage(pageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        } catch (Exception e) {
            throw new DataAccessException("【运营_用户_用户管理】获取分页列表失败",e);
        }
    }

    /**
     * <desc>
     *     设置用户状态。
     * <desc/>
     *
     * @param  userId 用户ID
     * @param  status 用户状态
     * @return
     * @author shengen.T
     * @createDate 2017/9/22
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.SET_USER_STATUS_LOG_DESC)
    @RequestMapping(path="/setUserStatus",method = RequestMethod.POST)
    @ResponseBody
    public Object setUserStatus( String userId, Integer status){
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            userId=request.getAttribute("userId")==null?null:request.getAttribute("userId").toString();
            status=request.getAttribute("status")==null?null:(Integer) request.getAttribute("status");
            if(StringUtils.isBlank(userId)) {
                return new MessageVO(OperateErrorCodeEnum.USER_ID_EMPTY_ERR.getErrorCode());
            }
            if((null == status) || (status != DataStatusEnum.NORMAL.getType() &&
                    status != DataStatusEnum.BAN.getType())){
                return new MessageVO(OperateErrorCodeEnum.USER_STATUS_ANVALID_ERR.getErrorCode());
            }
            boolean result = iOperateUserManageServiceClient.setUserStatus(userId,status);
            if(result){
                List<AppUserOwnProjectVO> userOwnProjectsInfo = iAppRegisterServiceClient.getUserOwnProjectsInfo(userId, "1");
                if (userOwnProjectsInfo != null && !userOwnProjectsInfo.isEmpty()) {
                    for (AppUserOwnProjectVO appUserOwnProjectVO : userOwnProjectsInfo) {
                        //查询当前项目信息
                        ProjectPO projectPO = iOperateProjectServiceClient.getOneProject(appUserOwnProjectVO.getProjectId());
                        // 平板优先离线，则插入同步数据
                        if (projectPO.getOnOffLinePadFace() != null) {
                            PadFaceSyncDataDTO padFaceSyncDataDTO = new PadFaceSyncDataDTO();
                            padFaceSyncDataDTO.setOperateType("0");
                            padFaceSyncDataDTO.setAppUserIds(userId);
                            padFaceSyncDataDTO.setProjectId(appUserOwnProjectVO.getProjectId());
                            // 用户禁用启用状态 1正常 5禁用 4冻结 2欠费
                            if(projectPO.getOnOffLinePadFace() == 1){
                                padFaceSyncDataDTO.setStatus(DataStatusEnum.NORMAL.getType().equals(status) ? "1" : "5");
                                iPadFaceSyncDataServiceClient.userOrFloorSync(padFaceSyncDataDTO);
                            }
                            padFaceSyncDataDTO.setStatus(DataStatusEnum.NORMAL.getType().equals(status) ? "1" : "2");
                            iPadFaceSyncDataServiceClient.userOrFloorSyncSmartCard(padFaceSyncDataDTO);
                        }
                    }
                }
                return new MessageVO(userId,BaseErrorCodeEnum.SUCCESS.getErrorCode());
            }else{
                return new MessageVO(BaseErrorCodeEnum.ERROR.getErrorCode());
            }
        }catch (Exception e){
            throw new DataAccessException("【运营_用户_用户管理】设置用户状态失败:",e);
        }

    }
}
